package app.client.netant.demo;

import java.util.ArrayList;
import java.util.List;

import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import app.client.netant.NetAnt;
import cn.lily.com.file.io.FileOutputManager;

/**
 * B站 - 网络爬虫
 * @author JiaJ
 * @date 2018年1月22日
 *
 */
public class BilbiBiliNetDemo {
	
	/**
	 * 地址信息
	 */
	private List<String> urls = new ArrayList<String>();
	
	private FileOutputManager fileManager;
	
	/**
	 * 构造
	 */
	public BilbiBiliNetDemo(String filePath){
		this.fileManager = new FileOutputManager();
		this.fileManager.loadFile(filePath);
	}
	
	/**
	 * 添加地址信息
	 * @param url
	 */
	public void addUrl(String url){
		this.urls.add(url);
	}
	
	public void analyse(){
		
		String pri = "https://www.bilibili.com/bangumi/play/ep";
		
		for(String url:this.urls){
			
			System.out.println("正在解析："+url);
			
			NetAnt netAnt = new NetAnt(url, 2000);
			
			Document document = netAnt.getDocument();
			
			String title = document.title();
			
			String canName = title.substring(0,title.indexOf("_"));
			
			Elements activePart = document.select(".main-container .main-inner .short-list .slider-part-wrapper > .slider-part > li");
			
			for(int i=0;i<activePart.size();i++){
				
				String name = activePart.get(i).attr("data-episode-id");
				String idx = activePart.get(i).children().get(0).attr("title");			
				String str = pri+name+"|"+canName+" "+idx+"\n";

				this.output(str);
			}

		}

	}
	
	private void output(String fileStr){
		this.fileManager.writeString(fileStr, "GBK");
	}
	
	public void free(){
		this.fileManager.free();
	}
	
	public static void main(String[] args) {
		
		/*BilbiBiliNetDemo netDemo = new BilbiBiliNetDemo("D:/any.txt");
		
		netDemo.addUrl("http://bangumi.bilibili.com/anime/5034");

		netDemo.analyse();
		
		netDemo.free();*/
		
		System.out.println("親友が気づかぬ内に彼女を寝取るボク パッケージ版".lastIndexOf(" "));
		
	}
	
	
}
